package com.igoal.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.igoal.entity.UserGoal;
import com.igoal.entity.UserGoalPhase;
import com.igoal.entity.UserGoalTask;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class UserGoalPhaseDTO extends BaseDTO<UserGoalPhase> {
	public static final int NORMAL_DEADLINE = 0;
	public static final int WARNING_DEADLINE = 1;
	public static final int MISSED_DEADLINE = 2;
	public static final int SUCCESS = 3;
	public static final int NONE_DEADLINE = -1;
	
	public static final int WARNING_DATE_NUMBER = 2;
	
	private Long goalId;
	private Long userId;
	private String phaseName;
	private Integer percentMeasure = new Integer(0);
	private String deadLine;
	private Boolean finished = Boolean.FALSE;
	private Integer index = new Integer(1);	
	
	private int deadLineStatus; //
	
	private List<UserGoalTaskDTO> tasks;
	
	public UserGoalPhaseDTO() {
		finished = Boolean.FALSE;
		index = new Integer(1);
		percentMeasure = new Integer(0);
		tasks = new ArrayList<UserGoalTaskDTO>();
		deadLineStatus = NORMAL_DEADLINE;
	}
	
	public UserGoalPhaseDTO(Integer index){
		this();
		this.index = new Integer(index);
	}
	
	public Integer getPercentMeasure() {
		return percentMeasure;
	}

	public void setPercentMeasure(Integer percentMeasure) {
		this.percentMeasure = percentMeasure;
	}

	public String getDeadLine() {
		return deadLine;
	}

	public void setDeadLine(String deadLine) {
		this.deadLine = deadLine;
	}

	public Boolean getFinished() {
		return finished;
	}

	public void setFinished(Boolean finished) {
		this.finished = finished;
	}

	public Long getGoalId() {
		return goalId;
	}

	public void setGoalId(Long goalId) {
		this.goalId = goalId;
	}

	public String getPhaseName() {
		return phaseName;
	}

	public void setPhaseName(String phaseName) {
		this.phaseName = phaseName;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public List<UserGoalTaskDTO> getTasks() {
		return tasks;
	}

	public void setTasks(List<UserGoalTaskDTO> tasks) {
		this.tasks = tasks;
	}
	
	public int getDeadLineStatus() {
		return deadLineStatus;
	}

	public void setDeadLineStatus(int deadLineStatus) {
		this.deadLineStatus = deadLineStatus;
	}

	@SuppressWarnings("unchecked")
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "taskList")
	public void initTasks(Object entity){
		if(entity != null && entity instanceof Set<?>){
			Set<UserGoalTask> taskList = (Set<UserGoalTask>) entity;
			for(UserGoalTask task : taskList){
				UserGoalTaskDTO taskDTO = new UserGoalTaskDTO();
				taskDTO.initFrom(task);
				tasks.add(taskDTO);				
			}
		}	
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "deadLine")
	public void initDeadLine(Object entity){
		if(entity != null && entity instanceof Date){
			Date deadLineDate = (Date) entity;
			deadLine = CalendarUtils.formatDate(deadLineDate, "dd/MM/yyyy");
			if(Boolean.TRUE.equals(finished)){
				this.deadLineStatus = SUCCESS;
			}else{
				this.deadLineStatus = CalendarUtils.checkTaskDeadLine(deadLineDate);
			}
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "userGoal")
	public void initUserGoal(Object entity){
		if(entity != null && entity instanceof UserGoal){
			UserGoal userGoal = (UserGoal) entity;
			this.userId = userGoal.getUser().getId();
			this.goalId = userGoal.getGoal().getId();			
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.TO, value = "deadLine")
	public Date copyDeadLine() {
		if (!StringUtils.isBlank(deadLine)) {
			Date date = CalendarUtils.parseDate(deadLine, "dd/MM/yyyy");
			return date;
		}

		return new Date();
	}
}
